package tinyuser.security;

import java.io.Serializable;

import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

/**
 * Implementation of {@link java.security.acl.Permission}. Extends the java interface by also giving each permission a
 * name.
 * 
 * @author Martin Algesten
 * 
 */
@SuppressWarnings("serial")
@MappedSuperclass
public class Permission implements java.security.acl.Permission, Serializable {

	@Id
	private String name;



	public Permission() {
	}



	/**
	 * Constructs a new permission by setting the name.
	 * 
	 * @param name
	 *          The name of the permission.
	 */
	public Permission(String name) {
		if (name == null)
			throw new IllegalArgumentException("name can't be null");
		this.name = name;
	}



	/**
	 * Returns the name of this permission. This is an addition to the java interface.
	 */
	public String getName() {
		return name;
	}



	protected void setName(String name) {
		this.name = name;
	}



	@Override
	public String toString() {
		return name;
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}



	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Permission other = (Permission) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}
